package task.snake.model;

import java.awt.Point;

import task.snake.model.Snake.Eat;

/**
 * Class RedFrog extends Frog class and describes RedFrog entity. When snake
 * eats red frog her length decrease and count increase on 2.
 * 
 * @author Nadezhda Makarkina
 * 
 */
public class RedFrog extends Frog {
	final String TYPE_OF_FROG = "Red";

	/**
	 * /**
	 * Initializes a newly created Frog object.
	 * 
	 * @param p position on play field
	 * @param sm snake model object
	 */
	public RedFrog(Point p, SnakeModel sm) {
		super(p, sm);
	}

	/**
	 * Method returns type of frog.
	 */
	@Override
	public String getTypeOfFrog() {
		return TYPE_OF_FROG;
	}

	/**
	 * Describe situation when red frog clashed by the snake.
	 */
	@Override
	public boolean onClash(Snake snake) {
		if (snake.getHeadLocation().equals(this.getHeadLocation())) {
			sm.setCounter(sm.getCounter() + 2);
			// tells snake that it has to grow up while net movement
			snake.setEat(Eat.RED_FROG);
			// kills frog and create new one
			for (int i = 0; i < sm.getFrogs().size(); i++) {
				// looks for the frog to kill
				if (sm.getFrogs().get(i) == this) {
					// stops the thread
					sm.getFrogs().get(i).setStopped();
					// sets cell free
					sm.setCell(sm.getFrogs().get(i).getHeadLocation(), null,
							false);
					// kills old frog
					sm.getFrogs().remove(i);
					// creates new frog
					sm.getFrogs().add(i, sm.createFrog());
					// sets cell busy
					sm.setCell(sm.getFrogs().get(i).getHeadLocation(), sm
							.getFrogs().get(i), true);
					// creates new thread for it
					Thread t = new Thread(sm.getFrogs().get(i));
					t.setName("Frog " + i);
					// and starts it
					t.start();
					break;
				}
			}
			return true;
		}
		return false;
	}
}
